﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml.Linq;

namespace ReporterApplication
{
    public class UserSettings
    {
        public string ReportFolder { get; set; }
        public string TemplateFile { get; set; }
        public string ResultFile { get; set; }
        public bool OpenResultInWord { get; set; }

        public static void Save(UserSettings settings)
        {
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(
               IsolatedStorageScope.User
               | IsolatedStorageScope.Assembly, null, null);

            using (IsolatedStorageFileStream oStream =
                new IsolatedStorageFileStream("settings.xml",
                FileMode.Create, isoStore))
            {

                using (StreamWriter writer = new StreamWriter(oStream))
                {
                    XDocument xdoc = new XDocument();
                    xdoc.Add(
                        new XElement(XName.Get("root"),
                        new XElement(XName.Get("ReportFolder"), settings.ReportFolder),
                        new XElement(XName.Get("TemplateFile"), settings.TemplateFile),
                        new XElement(XName.Get("ResultFile"), settings.ResultFile),
                        new XElement(XName.Get("OpenResultInWord"), settings.OpenResultInWord)
                        )                        
                    );
                    xdoc.Save(writer);
                }
            }


        }

        public static UserSettings Load()
        {
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(
               IsolatedStorageScope.User
               | IsolatedStorageScope.Assembly, null, null);

            if (isoStore.GetFileNames("settings.xml").Length == 1)
            {
                using (IsolatedStorageFileStream oStream =
                    new IsolatedStorageFileStream("settings.xml",
                    FileMode.Open, isoStore))
                {
                    using (StreamReader reader = new StreamReader(oStream))
                    {
                        XDocument xdoc = XDocument.Load(reader);

                        var result = new UserSettings();

                        result.ReportFolder = xdoc.Element(XName.Get("root")).Element(XName.Get("ReportFolder")).Value;
                        result.TemplateFile = xdoc.Element(XName.Get("root")).Element(XName.Get("TemplateFile")).Value;
                        result.ResultFile = xdoc.Element(XName.Get("root")).Element(XName.Get("ResultFile")).Value;
                        //result.ReportFolder = xdoc.Element(XName.Get("root")).Element(XName.Get("ReportFolder")).Value;

                        return result;
                    }
                }
            }



            return new UserSettings();
        }
    }
}
